package com.zhou.JavaAndroidFirm.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.adapter.NewListAdapter;
import com.zhou.JavaAndroidFirm.adapter.ScaleInAnimation;
import com.zhou.JavaAndroidFirm.app.AppConstant;
import com.zhou.JavaAndroidFirm.base.BaseFragment;
import com.zhou.JavaAndroidFirm.bean.NewsSummary;
import com.zhou.JavaAndroidFirm.constract.NewsListConstract;
import com.zhou.JavaAndroidFirm.event.ScollEvent;
import com.zhou.JavaAndroidFirm.model.NewsListModel;
import com.zhou.JavaAndroidFirm.presenter.NewsListPresenter;
import com.zhou.JavaAndroidFirm.widget.LoadingTip;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Administrator
 * @date 2018/7/16 0016
 * @des
 */
public class NewsFragment extends BaseFragment<NewsListModel, NewsListPresenter> implements NewsListConstract.View, OnRefreshLoadMoreListener {

    @BindView(R.id.irc)
    RecyclerView irc;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;
    private String mNewsId;
    private String mNewsType;
    private int startPage = 0;

    private List<NewsSummary> mData = new ArrayList<>();
    private NewListAdapter mAdapter;

    @Subscribe
    public void scrollTop(ScollEvent event) {
        irc.smoothScrollToPosition(0);
    }

    @Override
    protected void lazyFetchData() {
        refresh.autoRefresh();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.framents_news;
    }

    @Override
    protected void initPresenter() {
        mPresenter.onCreate(mModel, this);

    }

    @Override
    protected void initView(Bundle bundle) {
        if (getArguments() != null) {
            mNewsId = getArguments().getString(AppConstant.NEWS_ID);
            mNewsType = getArguments().getString(AppConstant.NEWS_TYPE);
        }
        irc.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new NewListAdapter(mContext, mData);
        mAdapter.openLoadAnimation(new ScaleInAnimation());
        refresh.setOnRefreshLoadMoreListener(this);

        irc.setAdapter(mAdapter);
    }

    @Override
    protected void initEvent() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void returnNewsListData(List<NewsSummary> newsSummaries) {
        mAdapter.addAll(newsSummaries);
        setUi(refresh, loadedTip, mAdapter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showTip(String tip) {
        mAdapter.notifyDataSetChanged();
        setUi(refresh, loadedTip, mAdapter);

    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        startPage += 20;
        mPresenter.getNewsListData(mNewsType, mNewsId, startPage);

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        startPage = 0;
        mData.clear();
        loadedTip.setVisibility(View.GONE);
        mPresenter.getNewsListData(mNewsType, mNewsId, startPage);

    }
}
